/*
*
* ████████╗░█████╗░███████╗██████╗░░█████╗░
* ╚══██╔══╝██╔══██╗██╔════╝██╔══██╗██╔══██╗
* ░░░██║░░░██║░░██║█████╗░░██████╦╝███████║
* ░░░██║░░░██║░░██║██╔══╝░░██╔══██╗██╔══██║
* ░░░██║░░░╚█████╔╝██║░░░░░██████╦╝██║░░██║
* ░░░╚═╝░░░░╚════╝░╚═╝░░░░░╚═════╝░╚═╝░░╚═╝
* 
*  :: 九方通逊-Henry ::   Copyright©2020 tofba.com  
* 
*/
package com.lmexpress.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * <正则表达式.>
 * 
 * @author Henry(fba02)
 * @version [版本号, 2020-12-7]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class RegexUtil {
	final static String US_PHONE = "^(\\+1)(\\s?\\d{3})(\\-)(\\d{3})(\\-)(\\d{4})(\\s?)(ext|EXT)(\\.)(\\s?\\d{5})$";
	// 1 314-282-9402 ext. 36055
	final static String US_PHONE2 = "^(\\s*)(1)(\\s?\\d{3})(\\-)(\\d{3})(\\-)(\\d{4})(\\s?)(EXT|ext)(\\.)(\\s?\\d{5})$";
	final static String US_POST_CODE = "^\\b\\d{5}\\b(?:[- ]{1}\\d{4})?$";

	public static boolean isMatch(String regex, String orginal) {
		if (StringUtils.isBlank(orginal)) {
			return false;
		}
		Pattern pattern = Pattern.compile(regex);
		Matcher isNum = pattern.matcher(orginal);
		return isNum.matches();
	}

	/**
	 * <US Phone Number>
	 * 
	 * @param orginal
	 * @return
	 * @author Henry(fba02)
	 * @version [版本号, 2020年103月20日]
	 * @see [类、类#方法、类#成员]
	 */
	public static boolean isUsPhone(String orginal) {
		return isMatch(US_PHONE, orginal) || isMatch(US_PHONE2, orginal);
	}

	/**
	 * <提取10位US电话>
	 * 
	 * @param orginal
	 * @return
	 * @author Henry(fba02)
	 * @version [版本号, 2020年3月25日]
	 * @see [类、类#方法、类#成员]
	 */
	public static String regexUsPhone(String orginal) {
		Pattern pattern = Pattern.compile(US_PHONE);
		Matcher matcher = pattern.matcher(orginal);
		String phone = "";
		while (matcher.find()) {
			phone = StringUtils.trim(matcher.group(2)) + StringUtils.trim(matcher.group(4)) + StringUtils.trim(matcher.group(6));
		}
		return phone;
	}

	/**
	 * 
	 * <提取10位US电话>
	 * 
	 * @param orginal
	 * @return
	 * @author Henry(fba02)
	 * @version [版本号, 2020-12-7]
	 * @see [类、类#方法、类#成员]
	 */
	public static String regexUsPhone1(String orginal) {
		Pattern pattern = Pattern.compile(US_PHONE2);
		Matcher matcher = pattern.matcher(orginal);
		String phone = "";
		while (matcher.find()) {
			phone = StringUtils.trim(matcher.group(3)) + StringUtils.trim(matcher.group(5)) + StringUtils.trim(matcher.group(7));
		}
		return phone;
	}

	/**
	 * 
	 * <US邮编>
	 * 
	 * @param orginal
	 * @return
	 * @author Henry(fba02)
	 * @version [版本号, 2020-12-7]
	 * @see [类、类#方法、类#成员]
	 */
	public static boolean isUsPostCode(String orginal) {
		return isMatch(US_POST_CODE, orginal);
	}
}